package context

import "reflect"

var Fc *FuncContext

func init() {
	Fc = NewFuncContext()
}

type FuncContext struct {
	methodContext        map[string]reflect.Value
	fuckFunctionInstance []interface{}
}

func NewFuncContext() *FuncContext {
	return &FuncContext{methodContext: make(map[string]reflect.Value)}
}

func (fc *FuncContext) Reg(custFunc interface{}) {
	fc.fuckFunctionInstance = append(fc.fuckFunctionInstance, custFunc)
}
func (fc *FuncContext) GetMethod(method string) reflect.Value {
	return fc.methodContext[method]
}
func (fc *FuncContext) Parse() {
	if fc.fuckFunctionInstance != nil && len(fc.fuckFunctionInstance) > 0 {
		for _, ffi := range fc.fuckFunctionInstance {
			fcType := reflect.ValueOf(ffi)
			methodType := reflect.TypeOf(ffi)
			methodTotal := methodType.NumMethod()

			for i := 0; i < methodTotal; i++ {
				fc.methodContext[methodType.Method(i).Name] = fcType.Method(i)
			}
		}
	}
}
